package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.nineoldandroids.view.ViewHelper;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.CommentAdapter;
import com.srwl.mytx.adapter.PictureAdapter;
import com.srwl.mytx.domain.Comment;
import com.srwl.mytx.domain.Topic;

import java.util.ArrayList;
import java.util.List;

public class InfoWindowView extends RelativeLayout {

    private Topic topic;
    private TextView tv_title;
    private TextView tv_content;
    private TextView tv_address;
    private RecyclerView rv_pictures;
    private RecyclerView rv_comments;
    private CommentAdapter commentAdapter;
    private List<Comment> commentList = new ArrayList<>();
    private List<String> pictures = new ArrayList<>();
    private PictureAdapter pictureAdapter;
    private ScaleGestureDetector scaleGestureDetector;
    private ScaleListener scaleListener;
    private float pivotX;
    private float pivotY;
    public InfoWindowView(Context context, Topic topic) {
        super(context);
        this.topic = topic;
        initView(context);
        initData(context);
    }

    public InfoWindowView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public InfoWindowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.infowindow_layout, this);
        tv_title = findViewById(R.id.tv_title);
        tv_content = findViewById(R.id.tv_content);
        tv_address = findViewById(R.id.tv_address);
        rv_pictures = findViewById(R.id.rv_picture);
        rv_comments = findViewById(R.id.rv_comment);
        scaleGestureDetector = new ScaleGestureDetector(context, new InfoWindowView.ScaleGestureListener());
    }

    private void initData(Context context) {
        tv_title.setText(topic.getTitle());
        tv_content.setText(topic.getContent());
        tv_address.setText(topic.getAddress());

        pictures.add("http://img.98tvs.com/2018/02/e41c3328ba085bf1cbf035f77a74ab7d.jpg");
        pictures.add("http://img.98tvs.com/2017/10/aeflrsuyBCDHJNWZ.jpg");
        pictureAdapter = new PictureAdapter(context, pictures);
        rv_pictures.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        rv_pictures.setAdapter(pictureAdapter);


    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        int pointerCount = event.getPointerCount();
        //根据情况选择是否拦截，这样子View 有机会获得MotionEvent
        if (pointerCount > 1) {
            return true;//当有多个手指在当前view上，拦截事件，进入下面的 onTouchEvent方法
        }
        return super.onInterceptTouchEvent(event);
        // return  true; //事件全部被拦截，子View 接不到事件
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

//        long currentMultiTouchTime = System.currentTimeMillis();
//        if (event.getAction()==MotionEvent.ACTION_DOWN&&currentMultiTouchTime - lastMultiTouchTime < 200) {
//            Log.d("InfoWindowView", "在200毫秒内有ACTION_DOWN到来" );
//            return false;
//        }

        return scaleGestureDetector.onTouchEvent(event);//让mScaleGestureDetector处理触摸事件

        //  return super.onTouchEvent(event);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
       pivotX=getX() + getWidth() / 2;
        pivotY= getBottom();

    }

    private class ScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener {
        private float scale;
        private float preScale = 1;// 默认前一次缩放比例为1


        @Override
        public boolean onScale(ScaleGestureDetector detector) {

            float previousSpan = detector.getPreviousSpan();// 前一次双指间距
            float currentSpan = detector.getCurrentSpan();// 本次双指间距
            if (currentSpan < previousSpan) {
                // 缩小
                scale = preScale - (previousSpan - currentSpan) / 1000;
                // scale -= 0.1f;
                Log.d("InfoWindowView", "缩小比例：" + scale);
            } else if (currentSpan > previousSpan) {
                // 放大
                scale = preScale + (currentSpan - previousSpan) / 1000;
                //  scale += 0.1f;
                Log.d("InfoWindowView", "放大比例：" + scale);
            }
            if (scale < 0.5) {
                scale = 0.5f;
            } else if (scale > 1) {
                scale = 1f;
            }
//            if (scaleListener != null) {
//                scaleListener.onScale(scale);
//            }
            View childAt = getChildAt(0);
            //设置缩放的中心点：下边缘的中点
            ViewHelper.setPivotX(InfoWindowView.this, getWidth()/2);
            ViewHelper.setPivotY(InfoWindowView.this, getHeight());

            ViewHelper.setScaleX(InfoWindowView.this, scale);// x方向上缩放
            ViewHelper.setScaleY(InfoWindowView.this, scale);// y方向上缩放
            float x = getScrollX();
            float y = getScrollY();
            float focusX = detector.getFocusX() + x;
            float focusY = detector.getFocusY() + y;



            return false;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            preScale = scale;// 记录本次缩放比例

        }
    }

    public void setScaleListener(ScaleListener scaleListener) {
        this.scaleListener = scaleListener;
    }

    public interface ScaleListener {
        void onScale(float scale);
    }
}
